<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="参数编号" prop="configCode">
        <el-input
          v-model="queryParams.configCode"
          placeholder="请输入参数编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="参数名称" prop="configName">
        <el-input
          v-model="queryParams.configName"
          placeholder="请输入参数名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="参数类型" prop="configType">
        <el-select v-model="queryParams.configType" placeholder="请选择参数类型" clearable>
          <el-option
            v-for="dict in dict.type.param_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="Python算法" prop="algorithmId" label-width="98px">
        <el-select v-model="queryParams.algorithmId" placeholder="请选择算法" clearable>
          <el-option
            v-for="algorithm in algorithmList"
            :key="algorithm.algorithmId"
            :label="algorithm.algorithmName + ' -v' + algorithm.algorithmVersion"
            :value="algorithm.algorithmId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="configList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="50"/>
      <el-table-column label="参数编号" align="center" prop="configCode" />
      <el-table-column label="参数名称" align="center" prop="configName" width="130" show-overflow-tooltip />
      <el-table-column label="参数类型" align="center" prop="configType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.param_type" :value="scope.row.configType"/>
        </template>
      </el-table-column>
      <el-table-column label="算法编码" align="center" prop="algorithmCode" width="130" show-overflow-tooltip />
      <el-table-column label="算法名称" align="center" prop="algorithmName" width="130" show-overflow-tooltip />
      <el-table-column label="算法类型" align="center" prop="algorithmType" width="130" show-overflow-tooltip>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.algorithm_type" :value="scope.row.algorithmType"/>
        </template>
      </el-table-column>
      <el-table-column label="显示顺序" align="center" prop="orderNum" />
      <el-table-column label="备注" align="center" prop="remark" width="180" show-overflow-tooltip />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改指标优化配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="88px">
        <el-form-item prop="configCode" label-width="98px">
          <span slot="label">
                <el-tooltip content="输入参数编号，如 --name" placement="top">
                <i class="el-icon-question"></i>
                </el-tooltip>
                参数编号
              </span>
          <el-input v-model="form.configCode" placeholder="请输入参数编号" />
        </el-form-item>
        <el-form-item label="参数名称" prop="configName">
          <el-input v-model="form.configName" placeholder="请输入参数名称" />
        </el-form-item>
        <el-form-item label="参数类型" prop="configType">
          <el-radio-group v-model="form.configType">
            <el-radio
              v-for="dict in dict.type.param_type"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="Python算法" prop="algorithmId" label-width="98px">
          <el-select v-model="form.algorithmId" placeholder="请选择算法" style="width: 100%" @change="handleAlgorithm">
            <el-option
              v-for="algorithm in algorithmList"
              :key="algorithm.algorithmId"
              :label="algorithm.algorithmName + ' -v' + algorithm.algorithmVersion"
              :value="algorithm.algorithmId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="显示顺序" prop="orderNum">
          <el-input-number v-model="form.orderNum" :min="0" :max="100" label="请输入显示顺序" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" maxlength="255" show-word-limit />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listConfig, getConfig, delConfig, addConfig, updateConfig } from "@/api/algorithm/config";
import { listAlgorithm, listAlgorithmData } from '@/api/system/algorithm'

export default {
  name: "Config",
  dicts: ['param_type', 'algorithm_type', 'sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 指标优化配置表格数据
      configList: [],
      // Python算法表格数据
      algorithmList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        configCode: null,
        configName: null,
        configType: null,
        algorithmId: null,
        algorithmCode: null,
        algorithmName: null,
        algorithmType: 'genetic_algorithm',
        orderNum: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        configCode: [
          { required: true, message: "参数编号不能为空", trigger: "blur" }
        ],
        configName: [
          { required: true, message: "参数名称不能为空", trigger: "blur" }
        ],
        configType: [
          { required: true, message: "参数类型不能为空", trigger: "change" }
        ],
        algorithmId: [
          { required: true, message: "请选择算法", trigger: "change" }
        ],
        algorithmType: [
          { required: true, message: "算法类型不能为空", trigger: "change" }
        ],
        orderNum: [
          { required: true, message: "显示顺序不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getAlgorithmList()
  },
  methods: {
    /** 查询指标优化配置列表 */
    getList() {
      this.loading = true;
      listConfig(this.queryParams).then(response => {
        this.configList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        configId: null,
        configCode: null,
        configName: null,
        configType: '0',
        algorithmId: null,
        algorithmCode: null,
        algorithmName: null,
        algorithmType: 'genetic_algorithm',
        orderNum: null,
        delFlag: null,
        status: '0',
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.configId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    getAlgorithmList() {
      listAlgorithmData({algorithmType: 'genetic_algorithm', status: '0'}).then(res => {
        this.algorithmList = res.data
      })
    },
    handleAlgorithm(val) {
      if (val) {
        this.algorithmList.forEach(item => {
          if (item.algorithmId === val) {
              this.form.algorithmCode = item.algorithmCode
              this.form.algorithmName = item.algorithmName
              this.form.algorithmType = item.algorithmType
          }
        })
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getAlgorithmList()
      this.open = true;
      this.title = "添加指标优化配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getAlgorithmList()
      const configId = row.configId || this.ids
      getConfig(configId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改指标优化配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.configId != null) {
            updateConfig(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addConfig(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const configIds = row.configId || this.ids;
      this.$modal.confirm('是否确认删除指标优化配置编号为"' + configIds + '"的数据项？').then(function() {
        return delConfig(configIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('algorithm/config/export', {
        ...this.queryParams
      }, `config_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
